import 'package:bruno/bruno.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


import '../../AppConfig/Config.dart';
import '../../AppConfig/RouteAct.dart';
import '../../http/NoLoadingGetApi.dart';
import '../../l10n/messages.dart';
import 'BillingDetails.dart';

class BillListView extends StatefulWidget {
  const BillListView({super.key});

  @override
  State<BillListView> createState() => _BillListViewState();
}

class _BillListViewState extends State<BillListView> {
  int chooseType=1;
  int timeHidden=0;
  int typeHidden=0;
  List billList=["(订金退款)苏州市吴中区-苏州市吴区","费用申请补偿","(订金退款)苏州市吴中区-苏州市吴区"];

  String timeTitle="时间筛选";
  List isChooseList=[];
  int pageNum=1;
  List dataList=[];
  String moneyType="";
  String status="";
  String timeStart="";
  String timeEnd="";
  @override
  void initState() {
    super.initState();
    DateTime now = DateTime.now();
    String month = '${now.month}'.padLeft(2, '0');
    int daysInMonth = getDaysInMonth(now.year, now.month);
    timeTitle = "${now.month}月账单";
    timeStart="${now.year}-${month}-01";
    timeEnd=="${now.year}-${month}-${daysInMonth}";
    print(timeStart);
    getBillList();
  }
  void _onRefresh(){
    pageNum=1;
    getBillList();
  }
  void onReachBottom(){
    pageNum=pageNum+1;
    getBillList();
  }
  void getBillList(){
    NoLoadinggetApiManager.getData(
      context, // 上下文对象，用于显示提示信息等
      'api/pay/wallet/shipper/bill/page', // 您的API端点
      {
        "current":pageNum,
        "size":20,
        "category":chooseType==2?"REFUND":chooseType==3?"EXPENDITURE":chooseType==4?"INCOME":chooseType==5?"IN":chooseType==6?"OUT":chooseType==6?"OTHER":"",
        "type":moneyType,
        "status":status,
        "periodFrom":timeStart,
        "periodTo":timeEnd

      }, // 查询参数，如果没有可以传递null
          (responseData)  {
        setState(() {
          if(pageNum==1){
            dataList=responseData['data']['records'];
          }else {
            if(responseData['data']['records'].isNotEmpty){
              dataList.addAll(responseData['data']['records']);
            }else{
              setState(() {
                pageNum=pageNum-1;
              });

            }
          }

        });
      },
    );
  }
  @override
  int getDaysInMonth(int year, int month) {
    return DateTime(year, month + 1, 0).day;
  }
  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context)!;
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: Text('账单',style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
      ),
      body: Column(
        children: [
          Column(
            children: [
              Container(
                child: Stack(
                  children: [
                    Column(
                      children: [
                        Container(
                          padding: EdgeInsets.only(top: 10.h,left: 10.w,right: 10.w,bottom: 10.h),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              BrnSmallMainButton(
                                title: '全部',
                                radius: 30,
                                bgColor: chooseType==1?MyColors.themeColor:Colors.white,
                                textColor: chooseType!=1?MyColors.textColor: Colors.white,
                                fontSize: 12.sp,
                                width: 60.w,
                                onTap: () {
                                  setState(() {
                                    chooseType=1;
                                    pageNum=1;
                                    getBillList();
                                  });
                                },
                              ),
                              BrnSmallMainButton(
                                title: '退款',
                                radius: 30,
                                bgColor: chooseType==2?MyColors.themeColor:Colors.white,
                                textColor:chooseType!=2?MyColors.textColor: Colors.white,
                                fontSize: 12.sp,
                                width: 60.w,
                                onTap: () {
                                  setState(() {
                                    chooseType=2;
                                    pageNum=1;
                                    getBillList();
                                  });
                                },
                              ),
                              BrnSmallMainButton(
                                title: '支出',
                                radius: 30,
                                bgColor: chooseType==3?MyColors.themeColor:Colors.white,
                                textColor:chooseType!=3?MyColors.textColor: Colors.white,
                                fontSize: 12.sp,
                                width: 60.w,
                                onTap: () {
                                  setState(() {
                                    chooseType=3;
                                    pageNum=1;
                                    getBillList();
                                  });
                                },
                              ),
                              BrnSmallMainButton(
                                title: '收入',
                                radius: 30,
                                bgColor: chooseType==4?MyColors.themeColor:Colors.white,
                                textColor: chooseType!=4?MyColors.textColor: Colors.white,
                                fontSize: 12.sp,
                                width: 60.w,
                                onTap: () {
                                  setState(() {
                                    chooseType=4;
                                    pageNum=1;
                                    getBillList();
                                  });
                                },
                              ),
                              GestureDetector(
                                child: Row(
                                  children: [
                                    Text("筛选",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                                    Icon(Icons.keyboard_arrow_down,color: MyColors.textColor,size: 14,),
                                  ],
                                ),
                                onTap: ()async{
                                  final result= await showModalBottomSheet(
                                    isScrollControlled: true,
                                    context: context,
                                    builder: (BuildContext context) {
                                      return BillFiltering(chooseList: isChooseList,
                                      );
                                    },
                                  );
                                  if(result!=null){
                                    setState(() {
                                      isChooseList=result;
                                      if(isChooseList[0].contains("退款"))chooseType=2;
                                      if(isChooseList[0].contains("支出"))chooseType=3;
                                      if(isChooseList[0].contains("收入"))chooseType=4;
                                      if(isChooseList[0].contains("充值"))chooseType=5;
                                      if(isChooseList[0].contains("提现"))chooseType=6;
                                      if(isChooseList[0].contains("其他"))chooseType=7;
                                      if(isChooseList[0].contains("退款"))chooseType=2;
                                      if(isChooseList[0].contains("支出"))chooseType=3;
                                      if(isChooseList[0].contains("收入"))chooseType=4;
                                      if(isChooseList[0].contains("充值"))chooseType=5;
                                      if(isChooseList[0].contains("提现"))chooseType=6;
                                      if(isChooseList[1].contains("订金"))moneyType="DEPOSIT";
                                      if(isChooseList[1].contains("运费"))moneyType="FREIGHT";
                                      if(isChooseList[1].contains("技术服务费"))moneyType="TECHNOLOGY";
                                      if(isChooseList[1].contains("保险费"))moneyType="PREMIUM";
                                      if(isChooseList[1].contains("加油"))moneyType="FUEL";
                                      if(isChooseList[2].contains("交易成功 "))status="02";
                                      if(isChooseList[2].contains("交易失败 "))status="03";
                                      if(isChooseList[2].contains("处理中 "))status="00";
                                      getBillList();
                                    });

                                  }
                                },
                              ),
                            ],
                          ),
                        ),

                        Container(
                          padding: EdgeInsets.only(left: 15.w,right: 15.w,bottom: 10.h,top: 10.h),
                          color: Colors.white,
                          child: Column(
                            children: [
                              GestureDetector(
                                child:Row(
                                  children: [
                                    Text(timeTitle,style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                                    Icon(Icons.keyboard_arrow_down, size: 11,color: MyColors.authTipColor,),  //               Icon(

                                  ],
                                ),
                                onTap: ()async{
                                  final result= await showModalBottomSheet(
                                    context: context,
                                    builder: (BuildContext context) {
                                      return dataTimeSheet(type: 1,
                                      );
                                    },
                                  );
                                  if (result != null) {

                                    setState(() {
                                      timeTitle=result;
                                      if(result.contains("至")){
                                        int indexOfZhi = result.indexOf("至");
                                        timeStart= result.substring(0, indexOfZhi);
                                        timeEnd= result.substring(indexOfZhi + 1);
                                        pageNum=1;
                                        getBillList();
                                      }else{
                                        timeStart=result+"-01";
                                        String endYear=result.substring(0, result.indexOf("-"));
                                        String endMonth=result.substring(result.indexOf("-") + 1);
                                        timeEnd=result+"-"+getDaysInMonth(int.parse(endYear), int.parse(endMonth)).toString();
                                        pageNum=1;
                                        getBillList();
                                      }
                                    });
                                  }
                                },
                              ),

                            ],
                          ) ,

                        ),
                        SizedBox(height: 1.h,),
                        EasyRefresh(
                          header:
                          ClassicHeader(
                            dragText: '下拉刷新',
                            armedText: '准备释放',
                            readyText: '加载中...',
                            processingText: '加载中...',
                            processedText: '刷新成功',
                            noMoreText: '暂无更多',
                            failedText: '刷新失败',
                            messageText: '刷新时间 %T',
                          ),
                          footer: ClassicFooter(
                            dragText: '加载更多',
                            armedText: '准备释放',
                            readyText: '加载中...',
                            processingText: '加载中...',
                            processedText: '加载成功',
                            noMoreText: '暂无更多',
                            failedText: '加载失败',
                            messageText: '加载时间 %T',
                          ),
                          onRefresh: _onRefresh,
                          onLoad:dataList.length>20*pageNum-1?onReachBottom:null,
                          child:
                          Container(
                            height: screenSize.height-200.h,
                            child: ListView.builder(

                                itemCount: dataList.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return GestureDetector(
                                    child:Container(
                                      padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h),
                                      color: Colors.white,
                                      child:  Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            children: [
                                              Row(
                                                children: [
                                                  Container(
                                                    width: 36.w,
                                                    height: 36.h,
                                                    child:  dataList[index]['targetAvatar']!=null&&dataList[index]['targetAvatar']!=""?
                                                    CircleAvatar(
                                                      radius: 15,
                                                      backgroundImage: NetworkImage(MyServiceUrl.imgHeader +dataList[index]['targetAvatar']),
                                                    )
                                                        :  CircleAvatar(
                                                      radius: 15,
                                                      backgroundImage: AssetImage("lib/Images/login/默认头像.jpeg"),
                                                    ),
                                                  ),
                                                  SizedBox(width: 8.w,),
                                                  Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: [
                                                      Container(
                                                        constraints: BoxConstraints(
                                                          // minWidth: 60.w,
                                                          maxWidth:screenSize.width/1.7,
                                                        ),
                                                        child:  Text("("+(dataList[index]['type']=="DEPOSIT"?"订金":dataList[index]['type']=="FREIGHT"?"运费":
                                                        dataList[index]['type']=="TECHNOLOGY"?"技术服务费":dataList[index]['type']=="PREMIUM"?"保险费":"油费")+((dataList[index]['category']=="IN"||dataList[index]['category']=="INCOME"||dataList[index]['category']=="REFUND")?"收入":"支出")+")"+dataList[index]['productDesc'],style: TextStyle(color: Colors.black,fontSize: 13.sp), overflow: TextOverflow.ellipsis,),
                                                      ),

                                                      SizedBox(height: 4.h,),
                                                      Text(dataList[index]['createTime'],style: TextStyle(color: MyColors.textColor,fontSize: 12.sp),),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                              Column(
                                                crossAxisAlignment: CrossAxisAlignment.end,
                                                children: [
                                                  Text(((dataList[index]['category']=="IN"||dataList[index]['category']=="INCOME"||dataList[index]['category']=="REFUND")?"+":"-")+dataList[index]['amount'],style: TextStyle(color: Colors.black,fontSize: 13.sp),),
                                                  SizedBox(height: 4.h,),
                                                  Text(dataList[index]['status']=="00"?"处理中":dataList[index]['status']=="01"?"交易中":dataList[index]['status']=="02"?"交易成功":
                                                  dataList[index]['status']=="03"?"交易失败":"交易未知",style: TextStyle(color: MyColors.textColor,fontSize: 12.sp),),
                                                ],
                                              ),
                                            ],
                                          ),
                                          SizedBox(height: 8.h,),
                                          Divider(height: 1.h,color: MyColors.backgreyColor,),
                                        ],
                                      ),
                                    ),
                                    onTap: (){
                                      Navigator.push( context, FadeRoute(page:BillingDetails(dataMap: dataList[index],
                                      )));

                                    },
                                  );



                                }
                            ),

                          ),
                        ),
                      ],

                    ),


                  ],
                ),

              ),


            ],
          ),

        ],
      ),
    );
  }
  Widget _buildPicker({
    required List<int> items,
    required FixedExtentScrollController controller,
    required ValueChanged<int> onChanged,
  }) {
    return Container(
      height: 150.h,
      width: (MediaQuery.of(context).size.width-40.w)/3,
      child: CupertinoPicker(
        itemExtent: 40.0,
        onSelectedItemChanged: (index) {
          onChanged(items[index]);
        },
        selectionOverlay: _buildSelectionOverlay(),
        children: items.map((item) => _buildPickerItem(item)).toList(),
        // 使用初始化的控制器
        scrollController: controller,
      ),
    );
  }

  Widget _buildPickerItem(int item) {
    // 将月份和日期格式化为两位数
    String formattedItem = item.toString().padLeft(2, '0');

    return Container(
      alignment: Alignment.center,
      child: Text(formattedItem),
    );
  }

  Widget _buildSelectionOverlay() {
    return Container(
      height: 40.0,
      decoration: BoxDecoration(
        // border: Border(
        //   top: BorderSide(color: Color.fromRGBO(220, 224, 231, 1), width: 1.0.h),
        //   bottom: BorderSide(color: Color.fromRGBO(220, 224, 231, 1), width: 1.0.h),
        // ),
      ),
    );
  }

  // 生成年份列表，包括当前年份和之后的5年
  List<int> generateYearList() {
    int currentYear = DateTime.now().year-25;
    List<int> years = List.generate(80, (index) => currentYear + index);
    return years;
  }
}

class dataTimeSheet extends StatefulWidget {
  const dataTimeSheet({super.key, required this.type});
  final int type;
  @override
  State<dataTimeSheet> createState() => _dataTimeSheetState();
}

class _dataTimeSheetState extends State<dataTimeSheet> {
  DateTime selectedDate = DateTime.now();
  int selectedYear = DateTime.now().year;
  int selectedMonth = DateTime.now().month;
  int selectedDay = DateTime.now().day;
  int selectedHour = DateTime.now().hour;
  late List<int> years;
  late FixedExtentScrollController yearController;
  late FixedExtentScrollController monthController;
  late FixedExtentScrollController dayController;
  late FixedExtentScrollController hourController;
  String startTime ="${DateTime.now().year}-${DateTime.now().month.toString().padLeft(2, '0')}-${DateTime.now().day.toString().padLeft(2, '0')}";
  String endTime ="结束时间";
  int timeType=1;
  int chooseTimeType=1;
  String chooseMonth="${DateTime.now().year}-${DateTime.now().month.toString().padLeft(2, '0')}";
  @override
  void initState() {
    super.initState();


    int selectedYear = DateTime.now().year;
    int selectedMonth = DateTime.now().month;
    int selectedDay = DateTime.now().day;
    int selectedHour = DateTime.now().hour;
    int currentYear = DateTime.now().year;
    years = List.generate(6, (index) => currentYear + index);
    // 设置初始滚动位置
    yearController = FixedExtentScrollController(initialItem: years.indexOf(selectedYear));
    monthController = FixedExtentScrollController(initialItem: selectedMonth - 1);
    dayController = FixedExtentScrollController(initialItem: selectedDay - 1);
    hourController = FixedExtentScrollController(initialItem: selectedHour);
  }
  @override
  int getDaysInMonth(int year, int month) {
    return DateTime(year, month + 1, 0).day;
  }

  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Container(
        padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.w,bottom: 40.h),
        height: 330.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Colors.white,
        ),
        child:Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(onPressed: (){
                  Navigator.pop(context);
                }, child: Text("取消",style: TextStyle(color: Colors.black,fontSize: 14.sp),)),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(Icons.access_time,color: MyColors.textColor,),
                    Text("选择时间",style:TextStyle(color: Colors.black,fontSize: 16.sp),),
                  ],
                ),

                TextButton(onPressed: (){
                  if(startTime=="开始时间"&&chooseTimeType==1){
                    BrnToast.show("请选择开始时间", context);
                  }else if(endTime=="结束时间"&&chooseTimeType==1){
                    BrnToast.show("请选择结束时间", context);
                  }else if(chooseMonth==""&&chooseTimeType==2){
                    BrnToast.show("请选择日期", context);
                  }else{
                    if(chooseTimeType==1)
                      Navigator.pop(context,startTime+"至"+endTime);
                    if(chooseTimeType==2)
                      Navigator.pop(context,chooseMonth);
                  }

                }, child: Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp))),
              ],
            ),
            Divider(height: 16.h,color: MyColors.backgreyColor,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(width: 10.w,),
                GestureDetector(
                  child:  Column(
                    children: [
                      Text("按日期选择",style: TextStyle(color:chooseTimeType==1?MyColors.themeColor: Colors.black,fontSize: 13.sp),),
                      Container(
                        height: 2.h,width:20.w,
                        color:chooseTimeType==1? MyColors.themeColor:Colors.white,
                      )
                    ],
                  ),
                  onTap: (){
                    setState(() {
                      chooseTimeType=1;
                    });
                  },
                ),

                GestureDetector(
                  child: Column(
                    children: [
                      Text("按月选择",style: TextStyle(color: chooseTimeType==2?MyColors.themeColor: Colors.black,fontSize: 13.sp),),
                      Container(
                        height: 2.h,width:20.w,
                        color:chooseTimeType==2? MyColors.themeColor:Colors.white,
                      )
                    ],
                  ),
                  onTap: (){
                    setState(() {
                      chooseTimeType=2;
                    });
                  },
                ),

                SizedBox(width: 10.w,),
              ],
            ),
            Divider(height: 16.h,color: MyColors.backgreyColor,),
            chooseTimeType==1?
            Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    SizedBox(width: 10.w,),
                    GestureDetector(
                      child: Container(
                        padding: EdgeInsets.only( top: 2.h,bottom: 2.h),
                        width: (screenSize.width-60.w)/2,
                        height: 30.h,

                        decoration: BoxDecoration(
                          border: Border.all(
                            color: timeType==1 ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                            width: 1.0, // 边框宽度
                          ),
                          color: timeType==1  ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                          borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                        ),
                        child:Center(
                          child:   Text(
                            startTime,
                            style: TextStyle(
                              fontSize: 13.sp,
                              color: timeType==1 ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                            ),
                          ),


                        ),
                      ),

                      onTap: (){
                        setState(() {
                          timeType=1;
                        });
                      },
                    ),

                    Text("-",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                    GestureDetector(
                      child: Container(
                        padding: EdgeInsets.only( top: 2.h,bottom: 2.h),
                        width: (screenSize.width-60.w)/2,
                        height: 30.h,

                        decoration: BoxDecoration(
                          border: Border.all(
                            color: timeType==2 ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                            width: 1.0, // 边框宽度
                          ),
                          color: timeType==2  ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                          borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                        ),
                        child:Center(
                          child:   Text(
                            endTime,
                            style: TextStyle(
                              fontSize: 13.sp,
                              color: timeType==2 ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                            ),
                          ),


                        ),
                      ),

                      onTap: (){
                        setState(() {
                          timeType=2;
                        });
                      },
                    ),

                    SizedBox(width: 10.w,),
                  ],
                ),
                Divider(height: 16.h,color: MyColors.backgreyColor,),

                Text("时间格式:年-月-日",style: TextStyle(color: Colors.black),),
                SizedBox(height: 4.h,),
                Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Color.fromRGBO(224, 227, 234, 1),
                      width: 1,
                    ),
                    borderRadius: BorderRadius.circular(5.0), // 可选，添加圆角
                  ),
                  child:  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      _buildPicker(
                        items: generateYearList(),
                        controller: yearController,
                        onChanged: (value) {
                          setState(() {
                            selectedYear = value;
                            timeType==1?startTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0')
                                :endTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0');
                          });
                        },
                      ),
                      _buildPicker(
                        items: List.generate(12, (index) => index + 1),
                        controller: monthController,
                        onChanged: (value) {
                          setState(() {
                            selectedMonth = value;
                            timeType==1?startTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0')
                                :endTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0');
                          });
                        },
                      ),
                      _buildPicker(
                        items: List.generate(getDaysInMonth(selectedYear, selectedMonth), (index) => index + 1),
                        controller: dayController,
                        onChanged: (value) {
                          setState(() {
                            selectedDay = value;
                            timeType==1?startTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0')
                                :endTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0');
                          });
                        },
                      ),
                      // _buildPicker(
                      //   items: List.generate(24, (index) => index),
                      //   controller: hourController,
                      //   onChanged: (value) {
                      //     setState(() {
                      //       selectedHour = value;
                      //       timeType==1?startTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0')+" "
                      //           :endTime=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0')+"-"+selectedDay.toString().padLeft(2, '0');
                      //     });
                      //   },
                      // ),
                    ],
                  ),
                ),
              ],
            )
                :Column(
              children: [

                Text("时间格式:年-月",style: TextStyle(color: Colors.black),),
                SizedBox(height: 4.h,),
                Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Color.fromRGBO(224, 227, 234, 1),
                      width: 1,
                    ),
                    borderRadius: BorderRadius.circular(5.0), // 可选，添加圆角
                  ),
                  child:  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      _buildPicker(
                        items: generateYearList(),
                        controller: yearController,
                        onChanged: (value) {
                          setState(() {
                            selectedYear = value;
                            chooseMonth=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0');
                          });
                        },
                      ),
                      _buildPicker(
                        items: List.generate(12, (index) => index + 1),
                        controller: monthController,
                        onChanged: (value) {
                          setState(() {
                            selectedMonth = value;
                            chooseMonth=selectedYear.toString()+"-"+selectedMonth.toString().padLeft(2, '0');

                          });
                        },
                      ),


                    ],
                  ),
                ),
              ],
            ),



          ],
        )



    );
  }
  Widget _buildPicker({
    required List<int> items,
    required FixedExtentScrollController controller,
    required ValueChanged<int> onChanged,
  }) {
    return Container(
      height: 150.0,
      width: 80.0,
      child: CupertinoPicker(
        itemExtent: 40.0,
        onSelectedItemChanged: (index) {
          onChanged(items[index]);
        },
        selectionOverlay: _buildSelectionOverlay(),
        children: items.map((item) => _buildPickerItem(item)).toList(),
        // 使用初始化的控制器
        scrollController: controller,
      ),
    );
  }

  Widget _buildPickerItem(int item) {
    // 将月份和日期格式化为两位数
    String formattedItem = item.toString().padLeft(2, '0');

    return Container(
      alignment: Alignment.center,
      child: Text(formattedItem),
    );
  }

  Widget _buildSelectionOverlay() {
    return Container(
      height: 40.0,
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(color: Color.fromRGBO(220, 224, 231, 1), width: 1.0.h),
          bottom: BorderSide(color: Color.fromRGBO(220, 224, 231, 1), width: 1.0.h),
        ),
      ),
    );
  }

  // 生成年份列表，包括当前年份和之后的5年
  List<int> generateYearList() {
    int currentYear = DateTime.now().year;
    List<int> years = List.generate(6, (index) => currentYear + index);
    return years;
  }
}
class BillFiltering extends StatefulWidget {
  const BillFiltering({super.key, required this.chooseList});
  final List chooseList;
  @override
  State<BillFiltering> createState() => _BillFilteringState();
}

class _BillFilteringState extends State<BillFiltering> {
  List orderTypeList=["充值","提现","退款","支出","收入","其他"];
  List moneyTypeList=["订金","技术服务费","运费","保险","加油"];
  List transactionTypeList=['交易成功','交易失败','处理中'];
  List isChooseList=["","",""];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.chooseList.isNotEmpty)
      isChooseList=widget.chooseList;
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Container(
      height: 450.h,
      width: screenSize.width,
      padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.w,bottom: 40.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10)),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [

          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              SizedBox(),
              Text("筛选",style: TextStyle(color: Colors.black,fontSize: 16.sp),),
              GestureDetector(
                child: Icon(Icons.close,color: MyColors.textColor,size: 25,),
                onTap: (){
                  Navigator.pop(context);
                },
              ),
            ],
          ),
          Divider(height: 20.h,color: MyColors.backgreyColor,),

          Text("运单类型",style: TextStyle(color: Colors.black,fontSize: 14.sp),),

          SizedBox(height: 10.h,),
          Container(

            child:  Wrap(

              spacing: 8.w, // 两个Container之间的水平间距
              runSpacing: 8.h, // 行与行之间的垂直间距
              children: orderTypeList.map((length) {
                bool? isSelected = isChooseList[0].toString().contains(length);
                return GestureDetector(
                  onTap: () {
                    // 处理点击事件
                    setState(() {
                      if (isSelected) {
                        // 如果已经被选中，则移除
                        isChooseList[0]="";
                      } else  {
                        // 如果未被选中且未达到最大选择数量，则添加
                        isChooseList[0]=length;
                      }
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.only( right: 10.w,left: 10.w,top: 2.h,bottom: 2.h),
                    width: (screenSize.width-44.w)/4,
                    height: 30.h,

                    decoration: BoxDecoration(
                      border: Border.all(
                        color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                        width: 1.0, // 边框宽度
                      ),
                      color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                      borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                    ),
                    child:Center(
                      child:   Text(
                        length,
                        style: TextStyle(
                          fontSize: 13.sp,
                          color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                        ),
                      ),


                    ),
                  ),
                );
              }).toList(),
            ),
          ),
          SizedBox(height: 10.h,),

          Text("费用类型",style: TextStyle(color: Colors.black,fontSize: 14.sp),),

          SizedBox(height: 10.h,),
          Container(

            child:  Wrap(
              spacing: 8.w, // 两个Container之间的水平间距
              runSpacing: 8.h, // 行与行之间的垂直间距
              children: moneyTypeList.map((length) {
                bool? isSelected = isChooseList[1].toString().contains(length);
                return GestureDetector(
                  onTap: () {
                    // 处理点击事件
                    setState(() {
                      if (isSelected) {
                        // 如果已经被选中，则移除
                        isChooseList[1]="";
                      } else  {
                        // 如果未被选中且未达到最大选择数量，则添加
                        isChooseList[1]=length;
                      }
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.only( top: 2.h,bottom: 2.h),
                    width: (screenSize.width-44.w)/4,
                    height: 30.h,
                    // constraints: BoxConstraints(
                    //   minWidth: 60.w,
                    //   maxWidth:200.w, // 限制文本最大宽度
                    // ), // Container宽度
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                        width: 1.0, // 边框宽度
                      ),
                      color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                      borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                    ),
                    child:Center(
                      child:   Text(
                        length,
                        style: TextStyle(
                          fontSize: 13.sp,
                          color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                        ),
                      ),


                    ),
                  ),
                );
              }).toList(),
            ),
          ),
          SizedBox(height: 10.h,),
          Text("交易状态",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
          SizedBox(height: 10.h,),
          Container(

            child:  Wrap(
              spacing: 8.w, // 两个Container之间的水平间距
              runSpacing: 8.h, // 行与行之间的垂直间距
              children: transactionTypeList.map((length) {
                bool? isSelected = isChooseList[2].toString().contains(length);
                return GestureDetector(
                  onTap: () {
                    // 处理点击事件
                    setState(() {
                      if (isSelected) {
                        // 如果已经被选中，则移除
                        isChooseList[2]="";
                      } else  {
                        // 如果未被选中且未达到最大选择数量，则添加
                        isChooseList[2]=length;
                      }
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.only( top: 2.h,bottom: 2.h),
                    width: (screenSize.width-44.w)/4,
                    height: 30.h,
                    // constraints: BoxConstraints(
                    //   minWidth: 60.w,
                    //   maxWidth:200.w, // 限制文本最大宽度
                    // ), // Container宽度
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                        width: 1.0, // 边框宽度
                      ),
                      color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                      borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                    ),
                    child:Center(
                      child:   Text(
                        length,
                        style: TextStyle(
                          fontSize: 13.sp,
                          color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                        ),
                      ),


                    ),
                  ),
                );
              }).toList(),
            ),
          ),
          Divider(height: 20.h,color: MyColors.backgreyColor,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              BrnSmallOutlineButton(
                title: '重置',
                lineColor: MyColors.themeColor,
                textColor: MyColors.themeColor,
                fontSize: 16.sp,
                radius: 30,
                width: (screenSize.width-30.w)/2,
                onTap: () {
                  setState(() {
                    isChooseList=["","",""];
                  });
                },
              ),
              BrnSmallMainButton(
                title: '确定',
                bgColor: MyColors.themeColor,
                textColor: Colors.white,
                fontSize: 16.sp,
                radius: 30,
                width: (screenSize.width-30.w)/2,
                onTap: () {
                  Navigator.pop(context,isChooseList);
                },
              )
            ],
          )
        ],
      ),
    );
  }
}
